package com.ocean.everyday.d008_maximumnumberofpairsinarray;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * https://leetcode.cn/problems/maximum-number-of-pairs-in-array/
 * <p>
 * 数组能形成多少数对
 *
 * @author linmiaolai@sanyygp.com<br>
 * @version 1.0<br>
 * @date 2023/02/16 <br>
 */
public class MaximumNumberOfPairsInArray {
    public static void main(String[] args) {
        MaximumNumberOfPairsInArray pairsInArray = new MaximumNumberOfPairsInArray();
        int[] nums = {1, 1, 2, 2, 2, 3, 3};
        int[] res = pairsInArray.numberOfPairs(nums);
        System.out.println(Arrays.toString(res));

    }


    public int[] numberOfPairs(int[] nums) {
        int[] answers = new int[2];
        if (nums.length <= 1) {
            answers[0] = 0;
            answers[1] = nums.length;
            return answers;
        }
        // 使用hash表
        Map<Integer, Boolean> map = new HashMap<>();
        int res = 0;
        for (int num : nums) {
            map.put(num, !map.getOrDefault(num, false)); // 不成对是设置为true
            // 如果是false 说明 取反为true 说明成对了
            if (!map.get(num)) {
                res++;
            }
        }
        answers[0] = res;
        answers[1] = nums.length - (res * 2);
        return answers;
    }
}
